agas
rm(list=ls())
data(diamonds)
head(diamonds)
data(mtcars)
head(mtcars)
# Inspired by the image-density plots of Ken Knoblauch
cars <- ggplot(mtcars, aes(mpg, factor(cyl))) #cyl numero de cilindros #mpg millas por galón
cars + geom_point() + geom_bin2d()
diamantes<-data.frame(diamonds[,"price"],diamonds[,"carat"],diamonds[,"cut"])
ggplot(diamantes,aes(x=price,y=carat,color=cut)) +
geom_point(shape=1)
ggplot(diamantes,aes(x=price,y=carat)) + xlim(0,6000) +
geom_bin2d()
Faceting Consiste en agrupar el dataframe según factores y pintar scatterplots de variables en función de esos agrupamientos:
data(mpg)
head(mpg)
qplot(data = mpg, x = displ, y = hwy, color = manufacturer, facets = ~class)
install.packages("plotly")
also installing the dependencies ‘httpuv’, ‘xtable’, ‘sourcetools’, ‘shiny’, ‘later’, ‘htmlwidgets’, ‘tidyr’, ‘hexbin’, ‘crosstalk’, ‘data.table’, ‘promises’
There is a binary version available but the source version is later:
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/httpuv_1.4.5.tgz'
Content type 'application/x-gzip' length 1699291 bytes (1.6 MB)
==================================================
downloaded 1.6 MB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/xtable_1.8-3.tgz'
Content type 'application/x-gzip' length 750457 bytes (732 KB)
==================================================
downloaded 732 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/sourcetools_0.1.7.tgz'
Content type 'application/x-gzip' length 133213 bytes (130 KB)
==================================================
downloaded 130 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/shiny_1.1.0.tgz'
Content type 'application/x-gzip' length 3587197 bytes (3.4 MB)
==================================================
downloaded 3.4 MB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/later_0.7.5.tgz'
Content type 'application/x-gzip' length 350117 bytes (341 KB)
==================================================
downloaded 341 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/htmlwidgets_1.3.tgz'
Content type 'application/x-gzip' length 792580 bytes (774 KB)
==================================================
downloaded 774 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/tidyr_0.8.1.tgz'
Content type 'application/x-gzip' length 637948 bytes (622 KB)
==================================================
downloaded 622 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/hexbin_1.27.2.tgz'
Content type 'application/x-gzip' length 838355 bytes (818 KB)
==================================================
downloaded 818 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/crosstalk_1.0.0.tgz'
Content type 'application/x-gzip' length 647834 bytes (632 KB)
==================================================
downloaded 632 KB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/data.table_1.11.8.tgz'
Content type 'application/x-gzip' length 1692925 bytes (1.6 MB)
==================================================
downloaded 1.6 MB
probando la URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/promises_1.0.1.tgz'
Content type 'application/x-gzip' length 288093 bytes (281 KB)
==================================================
downloaded 281 KB
The downloaded binary packages are in
/var/folders/0h/y5t8gnd91k56z5k3myfmgxvh0000gn/T//Rtmp1CrQ3o/downloaded_packages
installing the source package ‘plotly’
probando la URL 'https://cran.rstudio.com/src/contrib/plotly_4.8.0.tar.gz'
Content type 'application/x-gzip' length 1860673 bytes (1.8 MB)
==================================================
downloaded 1.8 MB
* installing *source* package ‘plotly’ ...
** package ‘plotly’ successfully unpacked and MD5 sums checked
** R
** data
*** moving datasets to lazyload DB
** demo
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded
* DONE (plotly)
The downloaded source packages are in
‘/private/var/folders/0h/y5t8gnd91k56z5k3myfmgxvh0000gn/T/Rtmp1CrQ3o/downloaded_packages’
library(reshape2) #facilita la trasnformación entre diferentes formatos de datos.
library(plotly) #https://plot.ly/r/
Attaching package: ‘plotly’
The following objects are masked from ‘package:plyr’:
arrange, mutate, rename, summarise
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
# Divide by levels of "sex", in the vertical direction
sp + facet_grid(sex ~ .)
ggplotly()
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
# Divide by levels of "sex", in the vertical direction
sp + facet_grid(. ~sex )
ggplotly() #vista optimizada y con muchas herramientas!!!
Dividimos por sex en vertical y day en horizontal:
sp + facet_grid(sex ~ day)
sp + facet_grid( ~ day, ncol=2)
Error in facet_grid(~day, ncol = 2) : unused argument (ncol = 2)
# A histogram of bill sizes
hp <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")
hp
# Histogram of total_bill, divided by sex and smoker
hp + facet_grid(sex ~ smoker)
# Same as above, with scales="free_y"
hp + facet_grid(sex ~ smoker, scales="free_y") #Escalas diferentes para la Y
# With panels that have the same scaling, but different range (and therefore different physical sizes)
hp + facet_grid(sex ~ smoker, scales="free", space="free")